package com.sinaapp.aidaka.server.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.sinaapp.aidaka.server.domain.UserInfo;
import com.sinaapp.aidaka.server.model.UserGender;

@Repository
public class UserInfoDao {

	private static final String USERINFO_TABLR_NAME = "t_user_info";

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 根据uId 查询对应的userInfo
	 * 
	 * @param uId
	 * @return
	 */
	public UserInfo getUserInfoByUID(final long uId) {
		String sql = "select " + "id, " + "u_nickname, " + "u_gender, "
				+ "u_description, " + "u_birthday, " + "u_address, "
				+ "u_region, " + "u_vocation, " + "u_qq_num, " + "u_mobile, "
				+ "u_small_picture, " + "u_big_picture, " + "u_create_time "
				+ "from " + USERINFO_TABLR_NAME + " where u_id = ?";

		final UserInfo userInfo = new UserInfo();
		jdbcTemplate.query(sql, new Object[] { uId }, new RowCallbackHandler() {

			public void processRow(ResultSet rs) throws SQLException {
				userInfo.setId(rs.getLong("id"));
				userInfo.setuId(uId);
				userInfo.setNickName(rs.getString("u_nickname"));
				userInfo.setGender(UserGender.getUserGenderByValue(rs.getInt("u_gender")));
				userInfo.setDescription(rs.getString("u_description"));
				userInfo.setBirthday(rs.getDate("u_birthday"));
				userInfo.setAddress(rs.getString("u_address"));
				userInfo.setRegion(rs.getString("u_region"));
				userInfo.setQqNum(rs.getString("u_qq_num"));
				userInfo.setMobile(rs.getString("u_mobile"));
				userInfo.setSmallPicture(rs.getString("u_small_picture"));
				userInfo.setBigPicture(rs.getString("u_big_picture"));
				userInfo.setCreateTime(rs.getDate("u_create_time"));
			}
		});
		if (userInfo.getId() == 0)
			return null;
		return userInfo;
	}

	/**
	 * 创建用户的UserInfo
	 * 
	 * @param uId
	 */
	public void newUserInfo(long uId) {
		String sql = "insert into " + USERINFO_TABLR_NAME
				+ " (u_id) values (?)";
		jdbcTemplate.update(sql, new Object[] { uId });
	}

	/**
	 * 更新UserInfo
	 * 
	 * @param userInfo
	 */
	public void updateUserInfo(UserInfo userInfo) {
		String sql = "update " + USERINFO_TABLR_NAME + " set "
				+ "u_nickname = ?, " + "u_gender = ?, " + "u_description = ?, "
				+ "u_birthday = ?, " + "u_address = ?, " + "u_region = ?, "
				+ "u_vocation = ?, " + "u_qq_num = ?, " + "u_mobile = ?, "
				+ "u_small_picture = ?, " + "u_big_picture = ?, "
				+ "u_create_time = ? " + "where u_id = ?";
		jdbcTemplate.update(
				sql,
				new Object[] { userInfo.getNickName(),
						userInfo.getGender().getValue(),
						userInfo.getDescription(), userInfo.getBirthday(),
						userInfo.getAddress(), userInfo.getRegion(),
						userInfo.getVocation(), userInfo.getQqNum(),
						userInfo.getMobile(), userInfo.getSmallPicture(),
						userInfo.getBigPicture(), userInfo.getCreateTime(),
						userInfo.getuId() });
	}

	/**
	 * 删除用户信息
	 * 
	 * @param uId
	 */
	public void deleteUserInfo(long uId) {
		String sql = "delete from " + USERINFO_TABLR_NAME + " where u_id = ?";
		jdbcTemplate.update(sql, new Object[] { uId });
	}
}
